<?php

//小站统计模型：访客与粉丝
class Sitestatis_model extends Model {

	function Sitestatis_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->model("accounts_model", "am");
	}
	
	//选择删除粉丝
	function delselectedfans($siteid, $delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		for ($i=0; $i<count($delid); $i++) {
			$userid = $delid[$i];
			$this->funs_unrecord($siteid, $userid);
		}
		return true;
	}
	
	//获取小站统计
	function get_sitestatis($siteid)
	{
		$sitestatis = array();
		$query = $this->db->get_where("site_statis", array("siteid"=>$siteid));
		if($query->num_rows()>0) {
			$sitestatis = $query->row_array();
		}
		return $sitestatis;
	}
	
	//粉丝新增统计
	function statis_newfuns($siteid, $rang = "week")
	{
		$start = $end = $newfuns = 0;
		switch($rang) {
			case "week"://上周
				$start = mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y"));
				$end = mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y"));
			break;
			case "month"://上月
				$start = mktime(0, 0 , 0,date("m")-1,1,date("Y"));
				$end = mktime(23,59,59,date("m")  ,0,date("Y")); 
			break;
		}
		if($start == 0 || $end == 0) return $newfuns;
		$query = $this->db->select("count(*) as count")->from("fans")->where(array("siteid"=>$siteid, "jointime >="=>$start, "jointime <="=>$end))->get();
		//echo $this->db->last_query();
		if($query->num_rows()>0) {
			$row = $query->row_array();
			$newfuns = $row["count"];
		}
		return $newfuns;
	}
	
	//获取小站粉丝列表
	function get_funs($siteid, $limit = 2, $offset = 0)
	{
		$funs = array();
		$query = $this->db->from("fans")->where(array("siteid"=>$siteid))->order_by("jointime", "desc")->limit($limit, $offset)->get();
		if($query->num_rows()>0) {
			$result = $query->result_array();
			//获取用户信息
			foreach($result as $row) {
				$user = $this->am->get($row["userid"]);
				if($user) {
					$user["jointime"] = $row["jointime"];
					$funs[] = $user;
				}
			}
		}
		return $funs;
	}
	
	//搜索小站粉丝列表
	function search_funs($siteid,$keyword)
	{
		$funs = array();
		$query = $this->db->query("select *, replace(username,'$keyword', '<em class=word_s>$keyword</em>') as title from rn_fans where siteid='$siteid' and LOCATE('$keyword', username)>0 order by id desc");
		if ($query->num_rows()>0) {
			$result = $query->result_array();
			//获取用户信息
			foreach($result as $row) {
				$funs[] = $this->am->get($row["userid"]);
			}
		}
		return $funs;
	}
	
	//获取某小站粉丝总数
	function get_sitefuns($siteid)
	{
		$fans = 0;
		$query = $this->db->get_where("site_statis", array("siteid"=>$siteid));
		if($query->num_rows()>0) {
			$row = $query->row_array();
			$fans = $row["fans"];
		}
		return $fans;
	}
	
	//获取我关注的总数
	function get_userfuns($userid)
	{
		if($userid == '0') return 0;
//		$query = $this->db->get_where("fans", array("userid"=>$userid));
		$this->db->select("fans.*,site.id,site.status");
		$this->db->from("fans");
		$this->db->join("site","site.id=fans.siteid");
		$this->db->where(array("fans.userid"=>$userid, "site.status"=>1));
		$query = $this->db->order_by("fans.id", "desc")->get();
		return $query->num_rows();
	}
	
	//关注小站:粉丝
	function funs_record($siteid, $userid)
	{
		if($this->is_fansed($siteid, $userid)) return false;
		//获取用户信息
		$user = $this->am->get($userid);
		$data = array(
			"siteid" => $siteid,
			"userid" => $userid,
			"username" => $user["username"],
			"jointime" => time(),
		);
		$result = $this->db->insert("fans", $data);
		
		//更新小站粉丝统计
		$result = $this->update_fans($siteid, $userid);
		return $result;
	}
	
	//取消关注小站
	function funs_unrecord($siteid, $userid)
	{
		//更新小站粉丝统计
		$this->db->delete("fans", array("siteid"=>$siteid, "userid"=>$userid));
		$result = $this->update_fans($siteid, $userid, "unset");
		return $result;
	}
	
	//记录小站访客数据:游客不记录
	function visit_record($siteid, $userid)
	{	
		//检测是否已经记录
		$recorded = $this->check_recorded($siteid, $userid);
		if(!$recorded) {//第一次记录
			$data = array(
				"siteid" => $siteid,
				"userid" => $userid,
				"intime" => time(),
			);
			$result = $this->db->insert("visits", $data);
			//更新小站访客总数
			$result = $this->update_visits($siteid);
			
		} else {//已记录：更新停留时间与访问时间
			$staytime = $recorded["staytime"] + (time() - $recorded["intime"]);
			$data = array(
				"intime" => time(),
				"staytime" => $staytime,
			);
			$this->db->where("id", $recorded["id"]);
			$result = $this->db->update("visits", $data);
			
			//计算新增访问时间差
			$staytime = $staytime - $recorded["staytime"];
			
			//更新小站访客停留时间总数
			$result = $this->update_visitstaytime($siteid, $staytime);
			
			//检测是否粉丝，如是即更新粉丝停留时间
			if($this->is_fansed($siteid, $userid)) {
				$result = $this->update_fanstaytime($siteid, $staytime);
			}
		}
		return $result;
	}
	
	//更新小站访客停留总时间
	function update_visitstaytime($siteid, $staytime)
	{
		$query = $this->db->get_where("site_statis", array("siteid"=>$siteid));
		if($query->num_rows()>0) {//更新总数
			$row = $query->row_array();
			$data = array(
				"visitstaytime" => $row["visitstaytime"]+$staytime,
			);
			$this->db->where("id", $row["id"]);
			$result = $this->db->update("site_statis", $data);
		}
	}
	
	//更新小站粉丝停留总时间
	function update_fanstaytime($siteid, $staytime)
	{
		$query = $this->db->get_where("site_statis", array("siteid"=>$siteid));
		if($query->num_rows()>0) {//更新总数
			$row = $query->row_array();
			$data = array(
				"fanstaytime" => $row["fanstaytime"]+$staytime,
			);
			$this->db->where("id", $row["id"]);
			$result = $this->db->update("site_statis", $data);
		}
	}
	
	//更新小站访客统计：总数
	function update_visits($siteid)
	{
		$query = $this->db->get_where("site_statis", array("siteid"=>$siteid));
		if($query->num_rows()>0) {//更新总数
			$row = $query->row_array();
			$data = array(
				"visits" => $row["visits"]+1,
			);
			$this->db->where("id", $row["id"]);
			$result = $this->db->update("site_statis", $data);
		} else {//新记录
			$q = $this->db->get_where("site", array("id"=>siteid));
			$site = $q->row_array();
			$data = array(
				"siteid" => $siteid,
				"classid" => $site["classid"],
				"visits" => 1,
			);
			$result = $this->db->insert("site_statis", $data);
		}
		return $result;
	}
	
	//更新小站分类
	function update_class($siteid, $classid)
	{
		$data = array(
			"classid" => $classid,
		);
		$this->db->where("siteid", $siteid);
		$this->db->update("site_statis", $data);
	}
	
	//更新小站粉丝统计
	function update_fans($siteid, $userid, $type = "set")
	{
		$result = false;
		switch($type) {
			case "set"://添加关注
				//停留时间
				$staytime = 0;
				$query = $this->db->get_where("visits", array("siteid"=>$siteid, "userid"=>$userid));
				if($query->num_rows()>0) {
					$row = $query->row_array();
					$staytime = $row["staytime"];
				}
				
				//获取小站统计：粉丝数
				$query = $this->db->get_where("site_statis", array("siteid"=>$siteid));
				if($query->num_rows()>0) {
					$row = $query->row_array();
					$fans = $row["fans"];
					$fanstaytime = $row["fanstaytime"];
					$data = array(
						"fans" => $fans+1,
						"fanstaytime" => $fanstaytime+$staytime,
					);
					$this->db->where("id", $row["id"]);
					$this->db->update("site_statis", $data);
					$result = $fans+1;
				} 
			break;
			case "unset"://取消关注
				$query = $this->db->get_where("site_statis", array("siteid"=>$siteid));
				if($query->num_rows()>0) {
					$row = $query->row_array();
					$fans = $row["fans"];
					$data = array(
						"fans" => $fans-1,
					);
					$this->db->where("id", $row["id"]);
					$this->db->update("site_statis", $data);
					$result = $fans-1;
				} 
			break;		
		}	
		return $result;
	}
	
	//获取今日访客数量
	function get_visits($siteid)
	{
		$today = time() - 24*3600;
		$visits = 0;
		$query = $this->db->select("count(*) as count")->from("visits")->where(array("intime >="=>$today, "siteid"=>$siteid))->get();
		//echo $this->db->last_query();
		if($query->num_rows()>0) {
			$row = $query->row_array();
			$visits = $row["count"];
		} 
		return $visits;
	}
	
	//获取今日访客列表
	function get_todayvisits($siteid, $limit = 2, $offset = 0)
	{
		$today = time() - 24*3600;
		$visits = array();
		$query = $this->db->select("*")->from("visits")->where(array("intime >="=>$today, "siteid"=>$siteid))->limit($limit, $offset)->get();
		//echo $this->db->last_query();
		if($query->num_rows()>0) {
			$result = $query->result_array();
			//获取用户信息
			foreach($result as $row) {
				$visits[] = $this->am->get($row["userid"]);
			}
		} 
		return $visits;
	}
	
	//检测是否已经记录
	function check_recorded($siteid, $userid)
	{
		$query = $this->db->get_where("visits", array("siteid"=>$siteid, "userid"=>$userid));
		if($query->num_rows()>0) {
			return $query->row_array();
		}
		return false;
	}
	
	//检测是否已经关注:自己除外
	function is_fansed($siteid, $userid)
	{
		if(!$userid) return false;
//		$query = $this->db->get_where("fans", array("siteid"=>$siteid, "userid"=>$userid, "self !="=>'1'));
		$query = $this->db->get_where("fans", array("siteid"=>$siteid, "userid"=>$userid));
		if($query->num_rows()>0) {
			return $query->row_array();
		}
		return false;
	}
}

/* End of file */